package com.air.repo.jdk8;

/**
 * Place Function on Here:
 *
 * @author yh 2018/8/21 10:20
 * @since: Jdk 1.8
 */
public class NotifyAllTest {


//         Object lock=new Object();
         String lock= "bbbbbbb";



     public    Thread createThread(String name) throws Exception{


        return new Thread(new Runnable() {

             public void run() {

                 if(name.equals("A")){
                     try {
                         Thread.sleep(200);
                     } catch (InterruptedException e) {
                         e.printStackTrace();
                     }
                 }else if(name.equals("B")){
                     try {
                         Thread.sleep(100);
                     } catch (InterruptedException e) {
                         e.printStackTrace();
                     }
                 }else if(name.equals("C")){
                     try {
                         Thread.sleep(400);
                     } catch (InterruptedException e) {
                         e.printStackTrace();
                     }
                 }

                 synchronized (lock){

                     try {
                             System.out.println(Thread.currentThread().getName()+" get Lock");
                             lock.wait();
//
                     } catch (Exception e) {
                         e.printStackTrace();
                     }

                     System.out.println(Thread.currentThread().getName()+" re run ");
                 }


             }
         },name);
     }

    public    Thread createThread1(String name) throws Exception{
        return new Thread(new Runnable() {

            public void run() {

                    try {
                        Thread.sleep(900);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }

                synchronized (lock){

                    try {
                        System.out.println(Thread.currentThread().getName()+" notify");
                        lock.notify();
//
                    } catch (Exception e) {
                        e.printStackTrace();
                    }

                }


            }
        },name);
    }


    public static void main(String[] args) throws  Exception{
        NotifyAllTest test=new NotifyAllTest();
        Thread t1=test.createThread("A");
        Thread t2=test.createThread("B");
        Thread t3=test.createThread("C");
        Thread t4=test.createThread1("4");
        t1.start();
        t2.start();
        t3.start();
        Thread.sleep(100L);
        t4.start();
    }



    public Thread cusumer(String name) throws Exception{
        return new Thread(new Runnable() {

            public void run() {
                synchronized (lock){

//                    System.out.println(Thread.currentThread().getName()+":relase before");

                    lock.notifyAll();


//                    System.out.println(Thread.currentThread().getName()+"relase after");
                }


            }
        },name);
    }


}
